package com.letoken.platform.repo.app.dao;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.repo.app.po.VideoCommentReply;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface VideoCommentRepliesMapper extends BaseMapper<VideoCommentReply> {


    @Update("update video_comment_replies set likes_user_ids = if(likes_user_ids is null, #{likeUserId} , CONCAT(likes_user_ids, ',', #{likeUserId}))   where id = #{id}")
    void like(@Param("id") Integer id, @Param("likeUserId") Integer likeUserId);

    @Update("update video_comment_replies set likes_user_ids = if(INSTR(likes_user_ids, ',') = 1 , replace(likes_user_ids, #{likeUserId}) , replace(likes_user_ids, CONCAT(',', #{likeUserId}) , '')  ) where id = #{id}")
    void unlike(@Param("id") Integer id, @Param("likeUserId") Integer likeUserId);

    @Select("select * from video_comment_replies where top_comment_id = #{topCommentId}  order by rank desc")
    Page<VideoCommentReply> page(@Param("page") Page<VideoCommentReply> page, @Param("topCommentId") Integer topCommentId);

    @Select("select min(`rank`) from video_comment_replies where top_comment_id = #{topCommentId}")
    Integer selectMinRankByTopCommentId(Integer  topCommentId);

    @Select("select min(`rank`) from video_comment_replies where comment_id = #{commentId}")
    Integer selectMinRankByCommentId(Integer commentId);

    @Select("select `rank` from video_comment_replies where id = #{id}")
    Integer selectRankByCommentId(Integer commentId);

    @Update("update video_comment_replies set `rank` = `rank` - 1 where top_comment_id = #{topCommentId} and `rank` < #{rank}")
    void decreaseRankByTopCommentIdAndLessThanRank(@Param("topCommentId") Integer topCommentId, @Param("rank") Integer rank);

    @Update("update video_comment_replies set `rank` = `rank` + 1 where top_comment_id = #{topCommentId} and `rank` > #{rank}")
    void increaseRankByTopCommentIdAndGreaterThanRank(@Param("topCommentId") Integer topCommentId, @Param("rank") Integer rank);

}
